package client_controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;




import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import client_gui.InitiateMarketingGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.AddressingCustomer;
import entity.Boss;
import entity.MarketingCampaign;
import entity.Product;


/**
 * @class InitiateMarketingController ,in this class the marketing manager Pairing between product type and category.
 * 
 * @param InitiateMarketingGUI links to GUI.
 * @param EmployeeLoginController links to employee controller. 
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 * @param MarketingEmployeeController links to marketing manager controller.
 */
public class InitiateMarketingController implements dbHandler,Serializable {

	private EmployeeLoginController elc;
	private MarketingManagerController mmc;
	private InitiateMarketingGUI img;
	private InitiateMarketingController imc;
	private Boss b ;
	private MarketingCampaign mc ;
	int cnt=0;
	
	public InitiateMarketingController(
			InitiateMarketingGUI initiateMarketingGUI,
			MarketingManagerController mmc, EmployeeLoginController elc) {
		imc = this;
		this.elc=elc;
		this.mmc = mmc ; 
		this.img = initiateMarketingGUI;
		getCampaignPatterns();
		this.img.getBtnInitiate().addActionListener(new InitiateListener());
		this.img.getBtnBack().addActionListener(new BackListener());
		this.img.getBtnCombine().addActionListener(new combineListener());
		
		// TODO Auto-generated constructor stub
	}

	/**
	 * This function getCampaignPatterns() get campaigns values from db
	 */
	private void getCampaignPatterns() {
		//get combo box for campaign pattern 
		this.setB(new Boss(null,"getCampainPatterns","InitiateMarketingController"));
		this.Search();
		
	}
	
	  /**
	 * the function HandleMsg get massage from server
	 * @param msg it item from the DB Usually comes as Boss
	 */
	public class BackListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			img.dispose();
			mmc.getMmg().setVisible(true);
			
			
		}
	}
	
	/**
	 * @class combineListener implements ActionListener make match and send to DB
	 * 
	 */
	public class combineListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			AddressingCustomer ac = new AddressingCustomer();
			ac.seteId(Integer.parseInt(img.getSalesman().getSelectedItem().toString()));
			ac.setcId(Integer.parseInt(img.getCustomers().getSelectedItem().toString()));
			ac.setCpId(mc.getMcId());
			imc.setB(new Boss(ac,"combine","InitiateMarketingController"));
			imc.Add();
			
		}
	}
	public class InitiateListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			//check fields 
			 mc = new MarketingCampaign();
			java.util.Date date,date1;
			try{
				mc.setMcId(Integer.parseInt(img.getCampaignId().getText()));
				mc.setName(img.getMcName().getText());
				mc.setPatternID(Integer.parseInt(img.getComboBox().getSelectedItem().toString()));
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				date =df.parse(img.getStartDate().getText());
				mc.setStartDate(date);
				//System.out.println(date);
				date1 =df.parse(img.getEndDate().getText());
				mc.setEndDate(date1);
				//System.out.println(date);
				if(date.getTime()<date1.getTime()){
					imc.setB(new Boss(mc,"initiatePatterns","InitiateMarketingController"));
					imc.Add();
				}
				else{
					img.getLblMsg().setText("plz check the dates");
					img.getLblMsg().setForeground(Color.red);
					img.getLblMsg().setVisible(true);
					
				}

			}catch (Exception e){
				e.printStackTrace();
				img.getLblMsg().setText("missing fields");
				img.getLblMsg().setVisible(true);
			}
			
		}
	}
	
	
	@Override
	public void Add() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
	}

	@Override
	public void Search() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
	}

	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * the function HandleMsg get massage from server
	 * @param msg it item from the DB Usually comes as Boss
	 */
	@Override
	public void HandleMsg(Object msg) {
		// TODO Auto-generated method stub
		this.setB((Boss) msg);
		if(this.getB().getCommand().equals("getCampainPatterns")){
		ArrayList <String> types ;
		types= (ArrayList<String>) this.getB().getObj();
		for(String i :types)
			img.getComboBox().addItem(i); 
			//System.out.println(i);
		}
		//allready in db //Campaign initialized - now we need to arrange customers per sales man 
		else if(this.getB().getCommand().equals("initiatePatterns")){
			if(this.getB().getMsg().equals("Campaign initialized")){
				//now choose sale man to customer 
				//
					//System.out.println(i);
				
				img.getBtnInitiate().setEnabled(false);
				img.getBtnBack().setEnabled(false);
				img.getLblMsg().setText("plz add sales man to customer");
				img.getLblMsg().setForeground(Color.green);
				img.getLblMsg().setVisible(true);
				int length= ((ArrayList<String>)getB().getObj()).size();
				for(int j= Integer.parseInt(((ArrayList<String>)getB().getObj()).get(length-1)); j<length-1;j++)
					img.getSalesman().addItem(((ArrayList<String>)getB().getObj()).get(j));
				for(int j=0;j< Integer.parseInt(((ArrayList<String>)getB().getObj()).get(length-1));j++){
					img.getCustomers().addItem(((ArrayList<String>)getB().getObj()).get(j));
				cnt++;}
				img.getBtnCombine().setEnabled(true);
				img.getLblcustomers().setText("#customer :"+cnt);
				if(cnt==0){ //check if there are no customers 
					img.getLblcustomers().setText("#customer :"+cnt);
					img.getBtnCombine().setEnabled(false);
					img.getBtnInitiate().setEnabled(true);
					img.getBtnBack().setEnabled(true);
					img.getLblMsg().setText("all customers added, u can continue now ");
					img.getLblMsg().setForeground(Color.green);
					img.getLblMsg().setVisible(true);}
				//for(String i : (ArrayList<String>)getB().getObj())
					//img.getCustomers().addItem(i);
			
			}
			else{
				img.getLblMsg().setText("could not add this campaign");
				img.getLblMsg().setForeground(Color.red);
				img.getLblMsg().setVisible(true);
			}
		}
		else if(this.getB().getCommand().equals("combine")){
			System.out.println("i am here");
			if(this.getB().getMsg().equals("added")){
				if(cnt==0){ //check if there are no customers 
					img.getLblcustomers().setText("#customer :"+cnt);
					img.getBtnCombine().setEnabled(false);
					img.getBtnInitiate().setEnabled(true);
					img.getBtnBack().setEnabled(true);
					img.getLblMsg().setText("all customers added, u can continue now ");
					img.getLblMsg().setForeground(Color.green);
					img.getLblMsg().setVisible(true);
				}
				else{
					//img.getCustomers().remove(img.getCustomers().getSelectedIndex());
					img.getCustomers().removeItemAt(img.getCustomers().getSelectedIndex());
					cnt--;
					img.getLblcustomers().setText("#customer :"+cnt);
					if(cnt==0){					img.getBtnCombine().setEnabled(false);
					img.getBtnInitiate().setEnabled(true);
					img.getBtnBack().setEnabled(true);
					img.getLblMsg().setText("all customers added, u can continue now ");
					img.getLblMsg().setForeground(Color.green);
					img.getLblMsg().setVisible(true);
					}
					
				}
				
				
			}
			else{

				img.getLblMsg().setText("could not add this to addressing customer .");
				img.getLblMsg().setForeground(Color.red);
				img.getLblMsg().setVisible(true);
			}
				
			
			
		}
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

	public EmployeeLoginController getElc() {
		return elc;
	}

	public MarketingManagerController getMmc() {
		return mmc;
	}

	public InitiateMarketingGUI getImg() {
		return img;
	}

	public InitiateMarketingController getImc() {
		return imc;
	}

	public Boss getB() {
		return b;
	}

	public MarketingCampaign getMc() {
		return mc;
	}

	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}

	public void setMmc(MarketingManagerController mmc) {
		this.mmc = mmc;
	}

	public void setImg(InitiateMarketingGUI img) {
		this.img = img;
	}

	public void setImc(InitiateMarketingController imc) {
		this.imc = imc;
	}

	public void setB(Boss b) {
		this.b = b;
	}

	public void setMc(MarketingCampaign mc) {
		this.mc = mc;
	}
	

}
